﻿#include"ProductManagementView.h"
#include"XQTablePagingWidget.h"
#include"XQAlgorithm.h"
#include"ProductView.h"
#include<QBoxLayout>
#include<QPushButton>
#include<QTableWIdget>
#include<QMenu>
ProductManagementView::ProductManagementView(QWidget* parent)
	:XQWidget(parent)
{
}

ProductManagementView::~ProductManagementView()
{
}

void ProductManagementView::setHideOperationButtons(bool hide)
{
	m_addButton->setVisible(!hide);
	m_modifyButton->setVisible(!hide);
	m_removeButton->setVisible(!hide);
}

XQTablePagingWidget* ProductManagementView::tableView() const
{
	return m_tableView;
}

ProductModel ProductManagementView::product(qint64 index)
{
	if (m_tableView->dataSize() <= index || index < 0)
		return ProductModel();
	auto& item = *m_tableView->datas()[index];
	ProductModel model;
	int nSel = 0;
	model.setProduct_id(item.data(nSel++).toULongLong());
	model.setName(item.text(nSel++));
	model.setType(item.text(nSel++));
	model.setPrice(item.data(nSel++).toDouble());
	model.setNumber(item.data(nSel++).toULongLong());
	nSel++;
	model.setMTime(item.data(nSel++).toDateTime());
	model.setCTime(item.data(nSel++).toDateTime());
	//qDebug() << model.mtime() << model.ctime();
	return model;
}

void ProductManagementView::setProduct(qint64 index, const ProductModel& model)
{
	if (m_tableView->dataSize() <= index || index < 0)
		return;
	auto& item = *m_tableView->datas()[index];
	item.clear();
	item.push_back(model.product_id());
	item.push_back(model.name());
	item.push_back(model.type());
	item.push_back(QString::number(model.price(),'f', 2) + "元", model.price());
	item.push_back(model.number());
	item.push_back(QString::number(model.totalPrice(), 'f', 2) + "元", model.totalPrice());
	item.push_back(model.mtime().toString("yyyy-MM-dd hh:mm:ss"),model.mtime());
	item.push_back(model.ctime().toString("yyyy-MM-dd hh:mm:ss"), model.ctime());
	m_type << model.type();
}

void ProductManagementView::setProduct(const QList<ProductModel>& list)
{
	clear();
	for (auto& data : list)
	{
		addProduct(data);
		m_type << data.type();
	}
}

void ProductManagementView::addProduct(const ProductModel& product)
{
	XQTablePagingItem item;
	item.push_back(product.product_id());
	item.push_back(product.name());
	item.push_back(product.type());
	item.push_back(QString::number(product.price(), 'f', 2) + "元", product.price());
	item.push_back(product.number());
	item.push_back(QString::number(product.totalPrice(), 'f', 2) + "元", product.totalPrice());
	item.push_back(product.mtime().toString("yyyy-MM-dd hh:mm:ss"), product.mtime());
	item.push_back(product.ctime().toString("yyyy-MM-dd hh:mm:ss"), product.ctime());
	m_tableView->push_backRow(item);
	m_type << product.type();
}

void ProductManagementView::removeProduct(const ProductModel& product)
{
	m_tableView->removeShowData_if([=](const XQTablePagingItem& item) {
		if (item[0].data.toULongLong() == product.product_id())
			return true;
		});
	//m_tableView->updataTable();
}

void ProductManagementView::clear()
{
	m_tableView->clearData();
	m_tableView->clearTable();
	m_tableView->showTable();
}

void ProductManagementView::showProduct(int page)
{
	m_tableView->showTable(page);
}

void ProductManagementView::updataTable()
{
	m_tableView->updataTable();
}

QStringList ProductManagementView::typeList()
{
	QStringList list;
	for (auto& data : m_type)
	{
		list << data;
	}
	return list;
}

void ProductManagementView::setContextMenuFunc()
{
	auto func = [=](const QPoint& pos) {
		//qDebug() << index.row() << index.column();
		QMenu* menu = new QMenu(m_tableView);
		menu->setAttribute(Qt::WA_DeleteOnClose);
		menuAddDefaultMenu(menu, pos);
		menu->popup(QCursor::pos());
		};
	m_tableView->setContextMenuFunc(func);
}

void ProductManagementView::menuAddDefaultMenu(QMenu* menu, const QPoint& pos)
{
	if (menu == nullptr)
		return;
	menuAddBuyProduc(menu, pos);
	menu->addSeparator();
	m_tableView->menuAddDefaultMenu(menu, pos);
}

void ProductManagementView::menuAddBuyProduc(QMenu* menu, const QPoint& pos)
{
	auto index = m_tableView->indexAt(pos);
	if (menu == nullptr || !index.isValid())
		return;
	menu->addAction("下单", [=] {
		auto product = this->product(m_tableView->showRow_toIndex(index.row()));
		auto view = new ProductView;
		view->init();
		view->setAttribute(Qt::WA_DeleteOnClose);
		connect(this, &QObject::destroyed, view, &QObject::deleteLater);
		connect(view, &XQWidget::closeSignals, [=] {this->setEnabled(true); });
		connect(view, &ProductView::okSignals, [=] {
			auto model = view->productModel();
			/*model.setCTime();
			model.setMTime();*/
			emit buyProductSignals(model);
			});
		centerShow(view, this);
		this->setEnabled(false);
		view->setOkButtonName("下单");
		view->setWindowTitle("下单商品");
		view->setReadOnly(ProductView::Type::IdType, true);
		view->setReadOnly(ProductView::Type::NameType, true);
		view->setReadOnly(ProductView::Type::TypeType, true);
		view->setReadOnly(ProductView::Type::PriceType, true);
		view->setReadOnly(ProductView::Type::TotalPriceType, true);
		view->setVisible(ProductView::Type::MtimeType, false);
		view->setVisible(ProductView::Type::CtimeType, false);
		setTranscribeWidget(CreateWidget::ProductBuyView, view);
		view->setProductModel(product);
		view->setNumber(1);
		emit buyProductCreateSignals(view);
		});
}

void ProductManagementView::init()
{
	XQWidget::init();
	init_table();
}

void ProductManagementView::addOperate()
{
	auto view = new ProductView;
	view->init();
	view->setAttribute(Qt::WA_DeleteOnClose);
	connect(this, &QObject::destroyed, view, &QObject::deleteLater);
	connect(view, &XQWidget::closeSignals, [=] {this->setEnabled(true); });
	connect(view, &ProductView::okSignals, [=] {
		auto model = view->productModel();
		model.setCTime();
		model.setMTime();
		emit addProductSignals(model);
		});
	centerShow(view, this);
	this->setEnabled(false);
	view->setOkButtonName("添加");
	view->setWindowTitle("添加商品");
	view->setReadOnly(ProductView::Type::IdType, true);
	view->setReadOnly(ProductView::Type::TypeType, true);
	view->setReadOnly(ProductView::Type::TotalPriceType, true);
	view->setVisible(ProductView::Type::MtimeType,false);
	view->setVisible(ProductView::Type::CtimeType, false);
	setTranscribeWidget(CreateWidget::ProductAddView, view);
	//auto typeList = this->typeList();
	//view->setTypeCompleter(typeList);
	//view->setTypeItems(typeList);
	emit addProductCreateSignals(view);
}

void ProductManagementView::modifyOperate()
{
	auto view = new ProductView;
	view->init();
	view->setAttribute(Qt::WA_DeleteOnClose);
	connect(this, &QObject::destroyed, view, &QObject::deleteLater);
	connect(view, &XQWidget::closeSignals, [=] {this->setEnabled(true); });
	connect(view, &ProductView::okSignals, [=] {
		emit modifyProductSignals(view->productModel());
		});
	centerShow(view, this);
	this->setEnabled(false);
	view->setOkButtonName("修改");
	view->setWindowTitle("编辑商品");
	view->setReadOnly(ProductView::Type::IdType, true);
	view->setReadOnly(ProductView::Type::TypeType, true);
	view->setReadOnly(ProductView::Type::TotalPriceType, true);
	view->setReadOnly(ProductView::Type::MtimeType, true);
	view->setReadOnly(ProductView::Type::CtimeType, true);
	setTranscribeWidget(CreateWidget::ProductModifyView, view);
	
	auto model = product(m_tableView->currentShowRow_toIndex());
	//view->setNumberRange(1,model.number );
	
	view->setProductModel(model);
	
	emit modifyProductCreateSignals(view);
}

void ProductManagementView::removeOperate()
{
	/*m_tableView->removeShowData(m_tableView->currentRow());
	m_tableView->updataTable();*/
	auto product=this->product(m_tableView->currentShowRow_toIndex());
	emit removeProductSignals(product);
}

void ProductManagementView::init_ui()
{
	
	XQWidget::init_ui();
	m_tableView = new XQTablePagingWidget(this);
	auto layout = new QBoxLayout(QBoxLayout::TopToBottom, widget());//从上到下
	layout->addWidget(init_operateButton());
	layout->addWidget(m_tableView);
	widget()->setLayout(layout);
}

void ProductManagementView::init_table()
{
	setContextMenuFunc();
	m_tableView->setHorizontalTitle({
		"编号","名称","类型","价格","数量","总价格","修改时间","创建时间"
		});
	m_tableView->tableWidget()->setSelectionBehavior(QAbstractItemView::SelectRows);
}

QWidget* ProductManagementView::init_operateButton()
{
	m_addButton = new QPushButton("添加");
	m_modifyButton = new QPushButton("编辑");
	m_removeButton = new QPushButton("删除");
	connect(m_addButton,&QPushButton::pressed,this,&ProductManagementView::addOperate);
	connect(m_modifyButton, &QPushButton::pressed, this, &ProductManagementView::modifyOperate);
	connect(m_removeButton, &QPushButton::pressed, this, &ProductManagementView::removeOperate);
	m_modifyButton->setEnabled(false);
	m_removeButton->setEnabled(false);
	connect(m_tableView->tableWidget(), &QTableWidget::itemSelectionChanged, [this] {
		if (!m_tableView->tableWidget()->selectedItems().isEmpty())
		{
			m_modifyButton->setEnabled(true);
			m_removeButton->setEnabled(true);
		}
		else
		{
			m_modifyButton->setEnabled(false);
			m_removeButton->setEnabled(false);
		}
		});
	auto layout = new QBoxLayout(QBoxLayout::LeftToRight);//从左到右
	layout->addWidget(m_addButton);
	layout->addWidget(m_modifyButton);
	layout->addWidget(m_removeButton);
	auto widget = new QWidget();
	setCssFile(widget, ":/XQWidget/style.css");
	widget->setLayout(layout);
	return widget;
	
}
